package com.spzx.order.receiver;

import com.rabbitmq.client.Channel;
import com.spzx.common.rabbit.constant.MqConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class TestReceiver {
    /**
     * 监听消息
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(
                    value = MqConst.EXCHANGE_TEST,
                    durable = "true",
                    type = "direct"
            ),
            value = @Queue(
                    value = MqConst.QUEUE_TEST,
                    durable = "true"
            ),
            key = MqConst.ROUTING_TEST
    ))
    public void test(Message message,String msg){
    log.info("接收到消息：{}",msg);
    log.info("消息内容：{}",new String(message.getBody()));
    }
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(
                    value = MqConst.EXCHANGE_TEST,
                    durable = "true",
                    type = "direct"
            ),
            value = @Queue(
                    value = MqConst.QUEUE_CONFIRM,
                    durable = "true"
            ),
            key = MqConst.ROUTING_CONFIRM
    ))
    public void confirm(String msg, Message message, Channel channel){
        log.info("接收到消息：{}",msg);
        log.info("消息内容：{}",new String(message.getBody()));
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            channel.basicAck(deliveryTag,true);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
